var api = require('../../utils/util.js').default;
var app = getApp();
Page({

  data: {
    // 导航栏组件
    TabCur: 0, // 0,1,2,3
    scrollLeft: 0,
    order_type: ['待配送', '配送中', '已完成', '退款/售后'],
    case0: [],
    case1: [],
    case2: [],
    case3: [],
  },

  // 清空订单
  clearOrder: function () {
    this.setData({
      case0: [],
      case1: [],
      case2: [],
      case3: []
    })
  },

  // 下拉刷新组件
  onPullDownRefresh: function () {
    this.clearOrder();
    this.getUserOrder();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 300);
  },

  // 初始化加载订单接口
  onLoad: function (options) {
    var type = options.type
    this.setData({
      TabCur: type,
      scrollLeft: (type - 1) * 60
    })
    this.clearOrder();
    this.getUserOrder(options);
  },

  // 导航栏组件
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },

  // 订单调试
  consoleOrder: function () {
    console.log("case0：待配送")
    console.log(this.data.case0)
    console.log("case1：配送中")
    console.log(this.data.case1)
    console.log("case2：已完成")
    console.log(this.data.case2)
    console.log("case3：退款/售后")
    console.log(this.data.case3)
  },

  // 获取订单接口
  getUserOrder: function (options) {

    var data = {
      openid: app.globalData.openid,
    }
    this.clearOrder();
    api.getUserOrder(data).then(res => {
      console.log(res.data.data)
      wx.setStorageSync('user_order', res.data.data);
      this.classifyOrder();
      this.consoleOrder();
    })
  },

  // 订单显示分类
  classifyOrder: function () {
    var arr = wx.getStorageSync('user_order');
    for (let i = arr.length - 1; i >= 0; i--) {
      var singleData = arr[i];
      console.log("singleData");
      console.log(singleData);
      switch (singleData.sever_type) { //服务类型
        case "1":
          singleData.sever_type = "快递代拿";
          break;
        case "2":
          singleData.sever_type = "外卖代拿";
          break;
        case "3":
          singleData.sever_type = "食堂代拿";
          break;
        case "4":
          singleData.sever_type = "超市代买";
          break;
        default:
          singleData.sever_type = "未知类型";
      }

      switch (singleData.status) { //接单情况
        case "000":
          singleData.status_show = "匹配中";
          break;
        case "001":
          singleData.status_show = "配送中";
          break;
        case "002":
          singleData.status_show = "已送达";
          break;
        case "003":
          singleData.status_show = "待接受";
          break;
        case "004":
          singleData.status_show = "退款";
          break;
        case "005":
          singleData.status_show = "退款中";
          break;
        case "006":
          singleData.status_show = "退款成功";
          break;
        default:
          singleData.status_show = "未知状态";
      }

      if (singleData.torider.length < 20) { //判断是否有骑手
        singleData.nickName = "【暂无骑手接单】";
      }
      var singleObject = {
        id:singleData.ID,
        tocustomer: singleData.tocustomer, //客户名
        sever_type: singleData.sever_type, //服务类型选择
        meet_place: singleData.meet_place, //交接地点
        unit_price: singleData.unit_price, //单价
        cus_details: singleData.cus_details, //客户的备注信息
        upload_time: singleData.upload_time, //订单提交时间
        torider: singleData.torider, //选择骑手
        status: singleData.status,
        status_show: singleData.status_show,
        img_src: singleData.head,
        rider_name: singleData.nickName,
      };
      switch (singleData.status) { //接单情况
        case "000": //匹配中 
        case "003": //待接受
          this.setData({
            case0: this.data.case0.concat(singleObject)
          });
          break;
        case "001": //配送中
          this.setData({
            case1: this.data.case1.concat(singleObject)
          });
          break;
        case "002": //已送达
          this.setData({
            case2: this.data.case2.concat(singleObject)
          });
          break;
        case "004": //可退款
        case "005": //退款中
        case "006": //退款成功
          this.setData({
            case3: this.data.case3.concat(singleObject)
          });
          break;
        default:
          console.log("归类出错");
      }
    }
  },

  // 退款
  refund: function (event) {
    console.log(event.target.dataset.id)
    var data = {
      id:event.target.dataset.id
    }
    api.refund(data).then(result=>{
      console.log(result)
    })
  }
})